"""
@author: yqx
@file: data_get_url.py
@time: 2024/8/2 23:58
"""
import time
import requests

# def download(url):
#     # 告诉目标站点字节信息
#     headers = {
#         # "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0"
#         # "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0"
#         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0"
#
#     }
#     try:
#         # 内部写有可能会出错的代码
#         response=requests.get(url,headers=headers)
#         response.raise_for_status()
#         # 设置编码格式
#         # response.encoding=response.apparent_encoding
#         response.encoding="UTF-8"
#         # 返回文本数据
#         time.sleep(3)
#         print(url,"访问成功")
#         return response.text
#     except Exception as ex:
#         # 如果出错了会执行这里
#         print(url,"访问错误:", ex)
#         return ""

import requests
import time


def download_cookie(url, cookies=None):
    # 告诉目标站点我们的请求来自哪个浏览器（伪装成浏览器）
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0"
    }
    try:
        # 如果提供了Cookie，则将其作为请求的一部分发送
        if cookies:
            response = requests.get(url, headers=headers, cookies=cookies)
        else:
            response = requests.get(url, headers=headers)

            # 检查响应状态码
        response.raise_for_status()

        # （可选）设置响应的编码格式为UTF-8（通常不需要，因为requests会自动处理）
        # response.encoding = "UTF-8"

        # 暂停3秒以模拟人类行为（可选）
        time.sleep(3)
        print(url, "访问成功")

        # 返回响应的文本内容
        return response.text
    except requests.exceptions.RequestException as ex:
        # 捕获requests库抛出的所有异常
        print(url, "访问错误:", ex)
        return ""

    # 示例用法：使用Cookie
# s = requests.Session()
# url = "https://www.zhipin.com/wapi/zpgeek/search/joblist.json?scene=1&query=&city=100010000&experience=&payType=&partTime=&degree=&industry=&scale=&stage=&position=100208&jobType=&salary=&multiBusinessDistrict=&multiSubway=&page=1&pageSize=30"  # 替换为需要Cookie的URL
# cookies="
# url="https://www.zhipin.com/wapi/zpgeek/search/joblist.json?scene=1&query=&city=100010000&experience=&payType=&partTime=&degree=&industry=&scale=&stage=&position=100106&jobType=&salary=&multiBusinessDistrict=&multiSubway=&page=1&pageSize=30"
# # SMqKbVIjYuD1iwYHtTTetjRyRIls_GU8OjV45Y7GdFL6cKY-TW4ZVsoiSf9orZtJ8rigAS6417GIGBOs33fQ~~; wbg=0; zp_at=Y6AD127NV_UPNKd6sDeaUKNCRch3h1O32iUIMva3CFM~; __zp_seo_uuid__=97a2f7e0-065f-4dc9-b979-2e2298df1e4b; __g=sem; __l=r=https%3A%2F%2Fcn.bing.com%2F&l=%2Fwww.zhipin.com%2Fsem%2F10.html%3F_ts%3D1724302057610%26sid%3Dsem%26qudao%3Dbing_pc_H120003UY5%26plan%3DBOSS-%25E5%25BF%2585%25E5%25BA%2594-%25E5%2593%2581%25E7%2589%258C%26unit%3Dboss%26keyword%3Dboss%26msclkid%3D88df3d76e0df148230465bb0223880ab&s=1&g=%2Fwww.zhipin.com%2Fsem%2F10.html%3F_ts%3D1724302057610%26sid%3Dsem%26qudao%3Dbing_pc_H120003UY5%26plan%3DBOSS-%25E5%25BF%2585%25E5%25BA%2594-%25E5%2593%2581%25E7%2589%258C%26unit%3Dboss%26keyword%3Dboss%26msclkid%3D88df3d76e0df148230465bb0223880ab&s=3&friend_source=0; collection_pop_window=1; __c=1724302057; __a=37782403.1718675716.1724234547.1724302057.206.19.4.4; bst=V2RN4nE-f0219oVtRuzRgZKiu47DrXxC4~|RN4nE-f0219oVtRuzRgZKiu47DrfwS8~; __zp_stoken__=1acffw4PDmCcNMgRVX1sMacKzXntiesK3e1DCsVZkwrNvRElMT1BKwrBPwrfCg8K6wp9HwpXCr8O0wqbCnE7CicKswqxLwoPCocKAwrLCnFTClMSQw73DjsScxIfCqsKxwpk3KgYBAAAFAwQFBQB1cg4OCwYBAAAFCA8ODgszLMKvwp0yMDowIkxPSAJOXVlBX0IHU0BDMDECDlZaMSwxMDA1wrwxwrUIwr82wrQIwro1wrNRMDg1NsK3DyUkwrEvBMK3AwLCscOEBMK1wrlUwrDCmVDDhlvDusO2w4rCscSwLDYzwrbEsDs3Fzw2NzkyMTk3NycxPsOAWsO%2Fw7DDjsK2LyI2Fj83Mjg0NzcyOjY5IzI%2BaiE3NSY6BQkKAw4mOsKxwrfCscOaNzI%3D; __zp_sseed__=Rob8J/u+UcQ2k0ZNfKKJePsCcPFp5lX4Ftb1FN/XuPo=; __zp_sname__=7711e6ff; __zp_sts__=1724302088820"
# cookie={
#     "lastCity": "100010000",
#     "gdxidpyhxdE": "2RCycwO44j4qmig9HU2qn95JtowXCx46IApT%2Ff9u%2FsaopsIceg79UUNkZZlTWz%5CZnNizUpszUd9U2dC19%5Ca%2By61tUa0q19VmmNLmGHg036Pc%5CZ2fUKedl2UDn%5CSwkrIKcHC6z1JtETGU8Rbg2l%2BmUWZ7vSVC%2BcbsX8SksJToucjWBoDH%3A1723472725873",
#     "wt2": "DvESMqKbVIjYuD1iwYHtTTetjRyRIls_GU8OjV45Y7GdFL6cKY-TW4ZVsoiSf9orZtJ8rigAS6417GIGBOs33fQ~~",
#     "wbg": "0",
#     "zp_at": "Y6AD127NV_UPNKd6sDeaUKNCRch3h1O32iUIMva3CFM~",
#     "__zp_seo_uuid__": "97a2f7e0-065f-4dc9-b979-2e2298df1e4b",
#     "__g": "sem",
#     "collection_pop_window": "1",
#     "__c": "1724302057",
#     "__a": "37782403.1718675716.1724234547.1724302057.206.19.4.4",
#     "bst": "V2RN4nE-f0219oVtRuzRgZKiu47DrXxC4~|RN4nE-f0219oVtRuzRgZKiu47DrfwS8~",
#     "__zp_stoken__": "1acffw4PDmCcNMgRVX1sMacKzXntiesK3e1DCsVZkwrNvRElMT1BKwrBPwrfCg8K6wp9HwpXCr8O0wqbCnE7CicKswqxLwoPCocKAwrLCnFTClMSQw73DjsScxIfCqsKxwpk3KgYBAAAFAwQFBQB1cg4OCwYBAAAFCA8ODgszLMKvwp0yMDowIkxPSAJOXVlBX0IHU0BDMDECDlZaMSwxMDA1wrwxwrUIwr82wrQIwro1wrNRMDg1NsK3DyUkwrEvBMK3AwLCscOEBMK1wrlUwrDCmVDDhlvDusO2w4rCscSwLDYzwrbEsDs3Fzw2NzkyMTk3NycxPsOAWsO%2Fw7DDjsK2LyI2Fj83Mjg0NzcyOjY5IzI%2BaiE3NSY6BQkKAw4mOsKxwrfCscOaNzI%3D",
#     "__zp_sname__": "7711e6ff",
#     "__zp_sts__": "1724302088820"
# }
# result = download_cookie(url, cookies=cookie)
# print(result)

